package com.zzw.zim.security.utils;

import com.zzw.zim.common.exception.ServerException;
import com.zzw.zim.common.utils.JsonUtils;
import com.zzw.zim.common.utils.Result;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

public class ResponseUtil {


	/**
	 * 使用response输出JSON
	 *
	 * @param response
	 * @param restResult
	 */
	public static <T> void out (HttpServletResponse response, Result<T> restResult) {
		try (ServletOutputStream out = response.getOutputStream()) {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/json;charset=UTF-8");
			out.write(JsonUtils.toJsonString(restResult).getBytes());
			out.flush();
			out.close();    //关闭流
		} catch (Exception e) {
			throw new ServerException("ResponseUtil Exception" + e.getMessage());
		}
	}
}
